# -*- coding: utf-8 -*-

from pymongo import MongoClient


class MongoDB():
    """
    输入一个数据库链接地址、数据库名称（字符串）、collection名称（字符串），不存在则创建
    """
    def __init__(self, uri, db, collection, pool_size=100):
        self._client = MongoClient(uri, maxPoolSize=pool_size)
        self.db = db
        self.collection = collection

    # get /creat a mongo database
    def get_database(self):
        if self._client:
            return self._client.get_database(self.db)
        else:
            raise Exception("MongoClient connection failed")

    # get /creat a mongo collection
    def get_collection(self):
        return self.get_database().get_collection(self.collection)

    def mongo_add(self, dic):
        self.get_collection().insert_one(dic)

    def mongo_addmany(self, dics):
        self.get_collection().insert_many(dics)

    def mongo_find(self, dic):
        return self.get_collection().find(dic)

    def mongo_update(self, filters, updater):
        self.get_collection().update(filters, {"$set": updater})

    # delete a database
    def mongo_deldb(self):
        self._client.drop_database(self.db)

    # delete a collection
    def mongo_delcol(self):
        self.get_database().drop_collection(self.collection)

    # delete all documents
    def mongo_delall(self):
        self.get_collection().remove()

    # delete a document
    def mongo_delone(self, dic):
        self.get_collection().remove(dic)

    def close(self):
        self._client.close()
